package vn.com.aptech.msh.utils;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 *
 * @author trietto
 */
public class MailHelper extends Thread {

    private static final String MAIL_USERNAME = "ngonhatphuong88@gmail.com";
    private static final String MAIL_PASSWORD = "chiyeumap";
    private String addFrom;
    private String addTo;
    private String sName;
    private String subject;
    private String content;
    private Properties props;

    public MailHelper(String addFrom, String addTo, String sName, String subject, String content) {
        this.addFrom = addFrom;
        this.addTo = addTo;
        this.sName = sName;
        this.subject = subject;
        this.content = content;
    }

    public void run() {
        try {
            props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "587");
            props.put("mail.debug", "true");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.localhost", "localhost");
            props.put("mail.mime.charset", "UTF-8");
            Session s = Session.getInstance(props, null);
            s.setDebug(true);
            MimeMessage message = new MimeMessage(s);
            InternetAddress from = new InternetAddress(addFrom, sName, "utf-8");
            InternetAddress to = new InternetAddress(addTo);
            MimeBodyPart messagePart = new MimeBodyPart();
            MimeMultipart multipart = new MimeMultipart();

            multipart.addBodyPart(messagePart);
            messagePart.setText(content,"utf-8");
            messagePart.setHeader("Content-Type","text/html; charset=\"utf-8\"");
            messagePart.setHeader("Content-Transfer-Encoding", "quoted-printable");            
            message.setSentDate(new Date());
            message.setFrom(from);
            message.addRecipient(Message.RecipientType.TO, to);
            message.setSubject(subject, "utf-8");
            message.setContent(multipart);
            Transport tr = s.getTransport("smtp");
            tr.connect("smtp.gmail.com", MAIL_USERNAME, MAIL_PASSWORD);
            message.saveChanges();
            tr.sendMessage(message, message.getAllRecipients());
            tr.close();
        } catch (MessagingException ex) {
            Logger.getLogger(MailHelper.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(MailHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void sendMail(String addFrom, String addTo, String sName, String subject, String content) {
        MailHelper mail = new MailHelper(addFrom, addTo, sName, subject, content);
        mail.start();
    }
}
